package com.hardson.toolkit.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;

import javax.swing.border.LineBorder;

import lombok.Getter;
import lombok.NonNull;

public class HardsonLineBorder extends LineBorder {
	private static final long serialVersionUID = 1L;

	protected RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);

	@Getter
	protected Insets insets;

	@Getter
	protected Insets outerInsets;

	@Getter
	protected Insets innerInsets;

	/** 是否不画线 */
	protected boolean empty = false;

	public HardsonLineBorder(final Color color, @NonNull final Insets insets, final boolean empty) {
		super(color);
		this.insets = insets;
		this.empty = empty;
		this.outerInsets = new Insets(0, 0, 0, 0);
		this.innerInsets = new Insets(0, 0, 0, 0);
	}

	/**
	 * 此方法返回的Insets对象，被布局管理器排除在容器空间之外。即，此对象将不占用容器的实际空间，表现为容器的相关尺寸将被额外加大。
	 */
	@Override
	public Insets getBorderInsets(final Component c, final Insets insets) {
		insets.set(this.insets.top + this.outerInsets.top + this.innerInsets.top,
				this.insets.left + this.outerInsets.left + this.innerInsets.left,
				this.insets.bottom + this.outerInsets.bottom + this.innerInsets.bottom,
				this.insets.right + this.outerInsets.right + this.innerInsets.right);
		return insets;
	}

	public void setLineColor(final Color color) {
		this.lineColor = color;
	}

	public HardsonLineBorder setInnerInsets(final Insets insets) {
		this.innerInsets = insets;
		return this;
	}

	public HardsonLineBorder setOuterInsets(final Insets insets) {
		this.outerInsets = insets;
		return this;
	}

	@Override
	public void paintBorder(final Component c, final Graphics g, final int x, final int y,
			final int width, final int height) {

		if (this.insets == null || this.empty) {
			return;
		}
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHints(this.rh);
		Color oldColor = g2d.getColor();
		g2d.setColor(this.lineColor);

		int fillWidth = c.getWidth() - this.outerInsets.left - this.outerInsets.right;
		int fillHeight = c.getHeight() - this.outerInsets.top - this.outerInsets.bottom;
		if (this.insets.top > 0) {
			g2d.fillRect(this.outerInsets.left, this.outerInsets.top, fillWidth, this.insets.top);
		}
		if (this.insets.left > 0) {
			g2d.fillRect(this.outerInsets.left, this.outerInsets.top, this.insets.left, fillHeight);
		}
		if (this.insets.bottom > 0) {
			int y0 = c.getHeight() - this.insets.bottom - this.outerInsets.bottom;
			g2d.fillRect(this.outerInsets.left, y0, fillWidth, this.insets.bottom);
		}
		if (this.insets.right > 0) {
			int x0 = c.getWidth() - this.insets.right - this.outerInsets.right;
			g2d.fillRect(x0, this.outerInsets.top, this.insets.right, fillHeight);
		}
		g2d.setColor(oldColor);
	}

	/**
	 * 构造四边皆为一个像素单位的边界
	 *
	 * @param color
	 * @return
	 */
	public static HardsonLineBorder of(final Color color) {
		return HardsonLineBorder.of(color, new Insets(1, 1, 1, 1), false);
	}

	public static HardsonLineBorder of(final Color color, final Insets insets,
			final boolean empty) {
		return new HardsonLineBorder(color, insets, empty);
	}

}
